<?php

namespace app\admin\controller;

use app\model\Admin;
use app\model\SiteSetting;
use support\Request;
use support\View;
use think\facade\Db;
use Webman\RedisQueue\Redis;
use \GatewayWorker\Lib\Gateway;
use Webman\Captcha\CaptchaBuilder;

class AuthController
{

	/**
	 * 登录
	 * @return [type] [description]
	 */
	public function login(Request $request)
	{
		if($request->method() == 'POST')
		{
            $username = $request->post('username');
            $password = $request->post('password');
            $captcha = $request->post('captcha');
            if (strtolower($captcha) !== $request->session()->get('captcha')) {

                return returnFail('验证码不正确');
            }
            $admin_info = Admin::getAdminInfo(['username'=>$username]);
            if(!empty($admin_info))
            {
                if($admin_info['status'] != 1)
                {
                    return returnFail('您的账户已被禁止登录');
                }
                if(md5($password.$admin_info['salt']) == $admin_info['password'])
                {
                    $session = $request->session();
                    //unset($admin_info['salt']);
                    unset($admin_info['password']);
                    $session->set('admin_info', $admin_info);

                    return returnSuccess('登录成功');
                }else{
                    return returnFail('账号或密码错误');
                }
            }else{
                return returnFail('未获取到用户信息');
            }
		}else{
            //网站配置
            $site_info = SiteSetting::getSite();
            View::assign('site_info', $site_info);
			return view('auth/login');
		}
	}

    /**
     * 生成验证码
     * @params string
     * @return
     */
    public function captcha(Request $request)
    {
        // 初始化验证码类
        $builder = new CaptchaBuilder;
        $builder->setMaxBehindLines(0);
        $builder->setMaxFrontLines(0);
        $builder->setTextColor(97,41,139);
        $builder->setBackgroundColor(243,251,254);
        // 生成验证码
        $builder->build();
        // 将验证码的值存储到session中
        $request->session()->set('captcha', strtolower($builder->getPhrase()));
        // 获得验证码图片二进制数据
        $img_content = $builder->get();
        // 输出验证码二进制数据
        return response($img_content, 200, ['Content-Type' => 'image/jpeg']);
    }

    /**
     * 验证验证码
     * @params string
     * @return
     */
    public function check(Request $request)
    {
        // 获取post请求中的captcha字段
        $captcha = $request->post('captcha');
        // 对比session中的captcha值
        if (strtolower($captcha) !== $request->session()->get('captcha')) {
            return json(['code' => 400, 'msg' => '输入的验证码不正确']);
        }
        return json(['code' => 0, 'msg' => 'ok']);
    }

    /**
     * 退出登录
     * @params string
     * @return
     */
    public function logout(Request $request)
    {
        $session = $request->session();
        $session->flush();
        return redirect('/admin/auth/login');
    }
}
